package com.tanhua.management.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.MessagesApi;
import com.tanhua.dubbo.server.pojo.Publish;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessagesService {


    @Reference(version = "1.0.0")
    private MessagesApi messagesApi;


    public String passPublish(String[] publishIds) {
        //先查看哪些没有审批通过过将那些没有通过审批
        List<ObjectId> publishIdList =new ArrayList<>();
        for (String publishId : publishIds) {
            publishIdList.add(new ObjectId(publishId));
        }
        //先去整理出未审核的动态
        List<Publish> passList = messagesApi.queryPulishForState(publishIdList,1);
        //拿到了未审核的动态ids
        List<Object> publishId = CollUtil.getFieldValues(passList,"id");
//将这些未审核的动态都改为审核通过
        String s = messagesApi.passPublish(publishId);
       if(s==null){
           return null;
       }
        return "审批通过，成功！";
    }

    public String rejectPublish(String[] publishIds) {
        List<ObjectId> publishIdList =new ArrayList<>();
        for (String publishId : publishIds) {
            publishIdList.add(new ObjectId(publishId));
        }

        List<Publish> rejectList = messagesApi.queryPulishForState(publishIdList,1);

        List<Object> publishId = CollUtil.getFieldValues(rejectList,"id");

        String s = messagesApi.rejectPublish(publishId);
        if(s==null){
            return null;
        }
        return "审批拒绝，成功！";
    }

    public String topPublish(String publishId) {
        Publish publish = messagesApi.queryPulishForTopState(publishId, 1);
        if(publish==null){
            return null;
        }
        String s = messagesApi.topPublish(publishId);
        if (s==null){
            return null;
        }
        return "置顶成功！";
    }

    public String untopPublish(String publishId) {
        Publish publish = messagesApi.queryPulishForTopState(publishId, 2);
        if(publish==null){
            return null;
        }
        String s = messagesApi.untopPublish(publishId);
        if (s==null){
            return null;
        }
        return "取消置顶成功！";
    }

    public String revocationPublish(String[] publishIds) {
        List<ObjectId> publishIdList =new ArrayList<>();
        for (String publishId : publishIds) {
            publishIdList.add(new ObjectId(publishId));
        }

        //state = 4 等于 已经审核通过或者审核拒绝的动态
        List<Publish> revocationList = messagesApi.queryPulishForState(publishIdList,4);

        List<Object> publishId= CollUtil.getFieldValues(revocationList,"id");

        String s = messagesApi.revocationPublish(publishId);
        if(s==null){
            return null;
        }
        return "消息撤销成功！";
    }
}
